<?php
$auth = Auth::getAuth('current_user');
$db = new Database;

$form_id = $_POST['FormID'];
$reference_field = $_POST['FieldReference'];
$reference_value = $_POST['FieldValue'];
$reference_filter = $_POST['FieldFilter'];
$HLData = $_POST['HLData'];
$HLData_json = "";
$HLAllow = $_POST['HLAllow'];
$HLType = $_POST['HLType'];
$field_column = $_POST['column_data'];
$current_form_fields_data = $_POST["current_form_fields_data"];
// print_r($current_form_fields_data);
try{
    // do something that can go wrong
    $HLData_json = json_decode($HLData);
}catch (Exception $e){
    //throw new Exception( 'Something really gone wrong', 0, $e);

}
try {
    $field_column_json = json_decode($field_column);
    // print_r($field_column_json);
} catch (Exception $e) {
    
}
try{
    // print_r($current_form_fields_data);
    $current_form_fields_data = json_decode($current_form_fields_data);
    // print_r($current_form_fields_data);
}catch(Exception $e){

}
function allowColumn($field_column_json,$column_field_name){
    if(isset($field_column_json) and count($field_column_json) >= 1){
        $passed_field_name = $column_field_name;
        $bool_checker = false;
        foreach ($field_column_json as $val) {//loop tr
            if($passed_field_name == $val){
                $bool_checker = true;;
                break;
            }
        }
        return $bool_checker;
    }else{
        return true; ///allow col field
    }
}


$formDoc = new Form($db, $form_id);
$headers = explode(",", $formDoc->active_fields);
$result = $db->query("SELECT " . $formDoc->active_fields . " FROM " . $formDoc->form_table_name . " WHERE " .
        $reference_filter . " = " . $db->escape($reference_value), "array");
?>

<table class="table_data display_data dataTable">
    <tr>
        <?php
        $column_index = -1;
        $collect_allowed_columns = array();
        foreach ($headers as $value) {
            $column_index = $column_index + 1;
            if(allowColumn($field_column_json,$value) == true){
                array_push($collect_allowed_columns, $column_index);
                echo '<th>' . $value . '</th>';
            }
            // echo '<th>' . $value . '</th>';
        }
        ?>
    </tr>
    <?php
    $temporary_store = "";
    foreach ($result as $key => $val) {//loop tr
        $td_hl_index = 0;
        $td_hl_index_val = "";


        ?>
        <tr>
        <?php
            $column_index = -1;
            if($HLType == "cell"){
                foreach ($val as $key_inner => $val_inner) { //loop td fn col
                    $column_index = $column_index + 1;
                    if(!empty($HLData_json) and $HLAllow == "true"){ //kapag naka enable ung checkboxs
                        
                        foreach ($HLData_json as $key2_inner) { // HL data loop
                            if($key2_inner->{"hl_fn"} == $key_inner){ // column pairing
                                if(evaluateHighlightRule($key2_inner, $val_inner, $val, $current_form_fields_data) == true ){
                                    $temporary_store = 'style="background-color:'.$key2_inner->{"hl_color"}.';"';
                                    break;
                                }
                            }
                        }
                        if(in_array($column_index, $collect_allowed_columns)){
                            echo '<td '.$temporary_store.' >' . $val_inner . '</td>';
                        }
                        $temporary_store = "";
                    }else{
                        if(in_array($column_index, $collect_allowed_columns)){
                            echo '<td>' . $val_inner . '</td>';
                        }
                    }
                }
            }else{
                foreach ($val as $key_inner => $val_inner) { //loop td fn col
                    $column_index = $column_index + 1;
                    $td_hl_index = $td_hl_index + 1;
                    if(!empty($HLData_json) and $HLAllow == "true"){ //kapag naka enable ung checkbox
                        foreach ($HLData_json as $key2_inner) { // HL data loop
                            if($key2_inner->{"hl_fn"} == $key_inner){ // column pairing
                                if(evaluateHighlightRule($key2_inner, $val_inner, $val, $current_form_fields_data) == true ){
                                    $temporary_store = 'style="background-color:'.$key2_inner->{"hl_color"}.';"';
                                    break;
                                }
                            }
                        }
                        if(!empty($temporary_store)){
                            break;
                        }
                    }
                }

                $count_td = -1;
                foreach ($val as $value) { //loop td fn col
                    $count_td = $count_td + 1;
                    if(in_array($count_td, $collect_allowed_columns)){
                        echo '<td '.$temporary_store.' >' . $value . '</td>';
                    }
                }
            }
        
        ?>
        </tr>
        <?php 
        
        $temporary_store = "";
    }







    function evaluateHighlightRule($key2_inner=null, $val_inner=null, $result=null, $current_form_fields_data){
        $condition_stat = false;
        $value2 = "";
        $value1 = "";
        if($key2_inner->{"value_type"} == "computed"){
            if(!empty($key2_inner->{"hl_value2"})){
                $formula_value_v2 = new Formula($key2_inner->{"hl_value2"});
                $formula_value_v2->DataFormSource[0] = $result;
                foreach ($current_form_fields_data as $value) {
                    $formula_value_v2->updateDataFormSource($value->{"f_name"}, $value->{"values"});
                }
                $value2 = $formula_value_v2->evaluate();
            }

            if(!empty($key2_inner->{"hl_value"})){
                $formula_value_v1 = new Formula($key2_inner->{"hl_value"});
                $formula_value_v1->DataFormSource[0] = $result;
                foreach ($current_form_fields_data as $value) {
                    $formula_value_v1->updateDataFormSource($value->{"f_name"}, $value->{"values"});
                }
                $value1 = $formula_value_v1->evaluate();
            }
        }

        if($key2_inner->{"condition_type"} == "range"){
            if($key2_inner->{"value_type"} == "static"){
                if($key2_inner->{"hl_value2"} >= $val_inner and $key2_inner->{"hl_value"} <= $val_inner){// value pairing
                    $condition_stat = true;
                }
            }else if($key2_inner->{"value_type"} == "computed"){
                if(floatval($value2) >= $val_inner and floatval($value1) <= $val_inner){// value pairing
                    $condition_stat = true;
                }
            }
        }elseif ($key2_inner->{"condition_type"} == "in_between") {
            if($key2_inner->{"value_type"} == "static"){
                if($key2_inner->{"hl_value2"} > $val_inner and $key2_inner->{"hl_value"} < $val_inner){// value pairing
                    $condition_stat = true;
                } 
            }else if($key2_inner->{"value_type"} == "computed"){
                if(floatval($value2) > $val_inner and floatval($value1) < $val_inner){// value pairing
                    $condition_stat = true;
                }
            }
        }elseif ($key2_inner->{"condition_type"} == "greater_than_equals") {
            if($key2_inner->{"value_type"} == "static"){
                if($val_inner >= $key2_inner->{"hl_value"}){
                    $condition_stat = true;
                }
            }else if($key2_inner->{"value_type"} == "computed"){
                if($val_inner >= floatval($value1)){
                    $condition_stat = true;
                }
            }
        }elseif ($key2_inner->{"condition_type"} == "greater_than") {
            if($key2_inner->{"value_type"} == "static"){
                if($val_inner > $key2_inner->{"hl_value"}){// value pairing
                    $condition_stat = true;
                }
            }else if($key2_inner->{"value_type"} == "computed"){
                if($val_inner > floatval($value1)){// value pairing
                    $condition_stat = true;
                }
            }
        }elseif ($key2_inner->{"condition_type"} == "less_than_equals") {
            if($key2_inner->{"value_type"} == "static"){
                if($val_inner <= $key2_inner->{"hl_value"}){// value pairing
                    $condition_stat = true;
                }
            }else if($key2_inner->{"value_type"} == "computed"){
                if($val_inner <= floatval($value1)){// value pairing
                    $condition_stat = true;
                }
            }
        }elseif ($key2_inner->{"condition_type"} == "less_than") {
            if($key2_inner->{"value_type"} == "static"){
                if($val_inner < $key2_inner->{"hl_value"}){// value pairing
                    $condition_stat = true;
                }
            }else if($key2_inner->{"value_type"} == "computed"){
                if($val_inner < floatval($value1)){// value pairing
                    $condition_stat = true;
                }
            }
        }else{
            if($key2_inner->{"value_type"} == "static"){
                if($key2_inner->{"hl_value"} == $val_inner){// value pairing
                    $condition_stat = true;
                }
            }else if($key2_inner->{"value_type"} == "computed"){
                if($value1 == $val_inner){// value pairing
                    $condition_stat = true;
                }
            }
        }
        return $condition_stat;
    }

    ?>
</table>















































<?php

/*
//kay jewel below
        ?>
        <tr>
        <?php
            $count_td = 0;
            foreach ($val as $value) { //loop td fn col
                $count_td = $count_td + 1;
                if($HLType == "cell"){
                    if($td_hl_index_val == $value){
                        echo '<td '.$temporary_store.' >' . $value . '</td>';
                    }else{
                        echo '<td>' . $value . '</td>';
                    }
                }else{
                    echo '<td '.$temporary_store.' >' . $value . '</td>';
                }
            }
        ?>
        </tr>
        <?php    
*/
?>